<?php


namespace app\Activity\controller;


use app\activity\model\ActivityItemRecord;
use app\activity\model\ActivityItemsInActivity;
use app\AdminRest;
use app\activity\model\ActivityItem as ActivityItemModel;
use app\activity\validate\ActivityItemValidate;


class AdminActivityItem extends AdminRest
{
    public function listAllWithPage()
    {
        $param = $this->_param;
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter = [
            ['uniacid', '=', $this->_uniacid],
        ];

        $model = new ActivityItemModel();
        $page_config['total'] = $model->countByFilter($filter);
        $rsts = $model->getListByFilter($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $result['data'] = $rsts;
        return $this->success($result);


    }

    public function getAll()
    {
        $model = new ActivityItemModel();
        $rst = $model->where([ ['status', '=', 1], ['uniacid', '=', $this->_uniacid]])
            ->order('top desc,id desc')
            ->select();
        return $this->success($rst);
    }

    public function createOne()
    {
        $validate = new ActivityItemValidate();
        $check = $validate->scene('create')->check($this->_input);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }
        $input = $this->_input;
        $input['uniacid'] = $this->_uniacid;
        $model = new ActivityItemModel();
        if ($model->where([['title' , '=',$input['title']], ['uniacid', '=', $this->_uniacid]],['status','>',-1])->count())
        {
            return $this->error('this item is exists');
//           dump( $model->where([['title' , '=',$input['title']], ['uniacid', '=', $this->_uniacid]],['status','>',-1])->find()->toArray());exit;
        }
        $res = $model->createOne($input);
        return $this->success($res);
    }

    public function updateOne()
    {
        $validate = new ActivityItemValidate();
        $check = $validate->scene('update')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }
        $item_id = $this->_param['id'];
        $input = $this->_input;
        $model = new ActivityItemModel();


        if ($model->where([
            ['id', '<>', $item_id],
            ['title', '=', $input['title']],
            ['uniacid', '=', $this->_uniacid]
        ])->count()) {
            return $this->error('this item is exists');
        }

        $info = $model->where(['id' => $item_id])->find()->toArray();

        $data = $model->updateByfilter(['id' => $item_id], $input);

        $items_model = new ActivityItemsInActivity();

        $record_model = new ActivityItemRecord();

        $items_model->where(['title'=>$info['title']])->update(['title'=>$input['title']]);

        $record_model->where(['title'=>$info['title']])->update(['title'=>$input['title']]);

        return $this->success($data);
    }

    public function getOne()
    {
        $validate = new ActivityItemValidate();
        $check = $validate->scene('get')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }
        $item_id = $this->_param['id'];
        $model = new ActivityItemModel();
        $data = $model->getOne(['id' => $item_id]);
        return $this->success($data);
    }

    public function changeStatus()
    {
        $validate = new ActivityItemValidate();
        $check = $validate->scene('updateStatus')->check($this->_param);
        if ($check == false)
        {
            return $this->error($validate->getError());
        }

        $item_id   = $this->_param['id'];
        $input = $this->_input;
        $model = new ActivityItemModel();
        $rst = $model->updateByfilter(['id' => $item_id], ['status' => $input['status']]);
        return $this->success($rst);
    }
}